<?php
class Library_Model_PhongBanMapper extends Library_Model_DataMapperAbstract
{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchPhongBanById($id)
	{
		$db = $this->getDb();
		$selectPhongban = $db->select()
				->from('tbl_phongban')
				->where('MaPhongBan = ?',$id);
		$phongban = $db->fetchRow($selectPhongban);
	
		return $this->_populate($phongban);
	
	}
	public function phongbanList()
	{
		$db = $this->getDb();
		$selectPhongban = $db->select()
						->from('tbl_phongban');
		$phongbans = $db->fetchAll($selectPhongban);
	
		$phongbanObjArray = array();
		foreach($phongbans as $phongban){
			$phongbanObjArray[] = $this->_populate($phongban);
		}
	
		return $phongbanObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_PhongBan();
		$obj->setId($data['MaPhongBan']);
		$obj->setTenphongban($data['TenPhongBan']);
		
		
		return $obj;		
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){	
		$db = $this->getDb();
		$data =array(
				'MaPhongBan' => $obj->getId(),
				'TenPhongBan' => $obj->getTenphongban()
					
				);
		$db->insert('tbl_phongban',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data =array(
				'MaPhongBan' => $obj->getId(),
				'TenPhongBan' => $obj->getTenphongban()
				);
		$where = $db->quoteInto('MaPhongBan = ?', $obj->getId());
		$db->update('tbl_phongban',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaPhongBan = ?',$id);
		$db->delete('tbl_phongban',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	}